﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sector7.Framework.DataDictionary.CORS;
using Sector7.Framework.DataDictionary.Utility;
using Sector7.Framework.Core;
using Sector7.Framework.Enums;
using Sector7.Framework.Consts;
using Sector7.Framework.DataDictionary.Agents;

namespace Sector7.Framework.DataDictionary.Windows
{
    /// <summary>
    /// AddDataGroupWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddDataGroupWindow : AddModeWindowBase
    {
        public S7_DataGroup DataGroup
        {
            get { return m_entity as S7_DataGroup; }
            set { m_entity = value; }
        }

        public AddDataGroupWindow()
        {
            InitializeComponent();

            FocusedControl = txbIdentifier;
        }

        protected override void AddData()
        {
            DataGroup.GroupIdentifier = txbIdentifier.Text;
            DataGroup.GroupName = txbName.Text;
            DataGroup.GroupType = cbbType.SelectedIndex + 1;
            DataGroup.MajorVersion = txbMajorVersion.Text;
            DataGroup.MinorVersion = txbMinorVersion.Text;
            DataGroup.RevisionNumber = txbRevisionNumber.Text;

            switch (m_crud)
            {
                case CrudType.Create:
                    DataGroup.GroupId = DataGroupServiceAgent.Add(DataGroup);
                    break;
                case CrudType.Update:
                    bool isChanged = false;
                    EntityDeltaBase delta = GetDelta(C_PrimaryKeyName.DATA_GROUP, ref isChanged);

                    if (isChanged)
                    {
                        DataGroupServiceAgent.Set(delta);
                    }
                    break;
                default:
                    break;
            }

            base.AddData();
        }

        protected override void InitializeForCreating()
        {
            txbIdentifier.Text = string.Empty;
            txbName.Text = string.Empty;
            txbMajorVersion.Text = string.Empty;
            txbMinorVersion.Text = string.Empty;
            txbRevisionNumber.Text = string.Empty;

            m_entity = new S7_DataGroup();

            base.InitializeForCreating();
        }

        protected override void InitializeForUpdating()
        {
            txbIdentifier.Text = DataGroup.GroupIdentifier;
            txbName.Text = DataGroup.GroupName;
            txbMajorVersion.Text = DataGroup.MajorVersion;
            txbMinorVersion.Text = DataGroup.MinorVersion;
            txbRevisionNumber.Text = DataGroup.RevisionNumber;
            cbbType.SelectedIndex = DataGroup.GroupType - 1;

            btnContinue.Visibility = Visibility.Hidden;
        }
    }
}
